package com.yuyou.train.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.PageParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;  import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.service.api.user.entity.UserThoughts;
import com.yuyou.train.user.service.IUserThoughtsService;
import com.yuyou.train.user.vo.req.ReqUserThoughts;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.tool.DefindBeanUtils;

import javax.servlet.http.HttpServletRequest;


@Validated
@RestController
@RequestMapping(value="/api/userThoughts")
public class UserThoughtsController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserThoughtsService userThoughtsService;

	@ApiOperation(value = "感想分页查询")
	@RequestMapping(value="/findUserThoughtsPage", method=RequestMethod.POST)
	public ReturnResult findUserThoughtsPage(int pageIndex, int pageSize){
		
		Map<String, Object> conditions=new HashMap<String, Object>();
		Pagination<UserThoughts> queryPgn = new Pagination<UserThoughts>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageSize(pageSize);
		queryPgn.setPageIndex(pageIndex);
		int total =userThoughtsService.findUserThoughtsPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total > 0){
			queryPgn.setTotal(total);
			List<UserThoughts> list=userThoughtsService.findUserThoughtsPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.success(queryPgn);
	}
	
	@ApiOperation(value = "新增感想")
	@RequestMapping(value="/addUserThoughts",method=RequestMethod.POST)
	public ReturnResult addUserThoughts(@Validated @ModelAttribute ReqUserThoughts reqUserThoughts){
		String userId="";
		UserThoughts userThoughts=new UserThoughts();
		DefindBeanUtils.copyProperties(reqUserThoughts, userThoughts);
		userThoughts.setUserId(userId);
		userThoughtsService.insert(userThoughts);
		return ReturnResult.success();
	}

	@ApiOperation(value = "更新感想")
	@RequestMapping(value="/updateThoughts",method=RequestMethod.POST)
	public ReturnResult updateThoughts(ReqUserThoughts reqUserThoughts){
		UserThoughts userThoughts=new UserThoughts();
		DefindBeanUtils.copyProperties(reqUserThoughts, userThoughts);
		userThoughtsService.updateByPrimaryKeySelective(userThoughts);
		return ReturnResult.success();
	}

	@ApiOperation(value = "我的感想(前台)")
	@RequestMapping(value="/myThoughtsPage", method=RequestMethod.POST)
	public ReturnResult<Pagination<UserThoughts>> myThoughtsPage(@Validated @ModelAttribute PageParams pageParams,
									   							 HttpServletRequest request){

		LoginRedisObj loginObj = this.getUserObj(request);

		Pagination<UserThoughts> queryPgn = new Pagination<>();
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("userId", loginObj.getUserId());
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total = userThoughtsService.findUserThoughtsPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total > 0){
			queryPgn.setTotal(total);
			List<UserThoughts> list = userThoughtsService.findUserThoughtsPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.success(queryPgn);
	}
}
